﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Profile;
using CommonApp;
using CommonApp.Models;
using IBS.Bll;
using IBS.Model;
using IBS.ServiceContracts;

namespace BankApp.Controllers
{
    [Authorize(Roles = "Operator")]
    public class PaymentsController : Controller
    {
        private IKnowledgeBaseService knowledgeBaseService;
        private ICardService cardService;
        private IPaymentService paymentService;
        private readonly OperationStep[] steps = new[]
                                           {
                                               new OperationStep { Uid="cardsMenuItem", Name = "Cards", GoTo = "/Payments"},
                                               new OperationStep
                                               { 
                                                   Uid="paymentMenuItem", 
                                                   Name = "Payment selection",
                                                   IsVisible = false,
                                                   GoTo = "/Payments/SelectPayment"
                                                   
                                               }
                                           };

        public PaymentsController()
        {
            var container = new IbsContainer();
            this.cardService = container.CardService;
            this.knowledgeBaseService = container.KnowledgeBaseService;
            this.paymentService = container.PaymentService;
        }

        public ActionResult Index(int id)
        {
            ViewBag.Steps = steps;
            ViewBag.ActiveStep = steps[0];

            var client = knowledgeBaseService.GetClientById(id);

            IEnumerable<Card> cards = null;

            try
            {
                cards = cardService.GetClientsCard(client, c => true);
            }
            catch (BusinessException be)
            {
                ModelState.AddModelError("", be.Message);
            }

            var profile = ProfileBase.Create(client.LastName);
            ViewBag.SelectedCardIndex = profile["CardIndex"];
            ViewBag.UserId = client.Id;
            return View(cards);
        }

        [HttpPost]
        public ActionResult SelectPayment(string cardNumber, int userId)
        {
            var client = knowledgeBaseService.GetClientById(userId);

            var profile = ProfileBase.Create(client.LastName);
            //var cards = cardService.GetCards();
            //var cardIndex = cards.IndexOf(cards.Single(c => c.Number == cardNumber));
            profile["CardIndex"] = "0";
            profile.Save();

            ViewBag.Steps = steps;
            steps[1].IsVisible = true;
            ViewBag.ActiveStep = steps[1];
            ViewBag.CardNumber = cardNumber;
            ViewBag.FormGoTo = Url.Action("Pay");
            // ViewBag.EnsureSecurity = true;

            return View();
        }

        public ActionResult Detail(int id, string cardNumber)
        {
            var p = new PaymentXmlBuilder();
            var payment = paymentService.GetPaymentById(id);
            var detailXml = "<form method='post' action ='/Payments/Pay'>" + p.PaymentDetailHtml(payment, cardNumber) + "</form>";
            return Content(detailXml, "text/plain");
        }
        public ActionResult Tree()
        {
            var p = new PaymentXmlBuilder();
            var EripPayments = paymentService.GetEripPayments();

            var xmlTree = p.PaymentsTreeXml(EripPayments);
            return this.Content(xmlTree, "text/xml");
        }


        public ActionResult Pay(FormCollection collection)
        {
            if (ModelState.IsValid)
            {
                var paymentId = int.Parse(collection["paymentId"]);
                var cardNumber = collection["card"];
                var ISO = collection["currency"];
                var amount = Decimal.Parse(collection["amount"]);
                try
                {
                    paymentService.PayPayment(cardNumber, paymentId, "", amount, ISO, User.Identity.Name);
                }
                catch (BusinessException be)
                {
                    ModelState.AddModelError("", be.Message);
                }
            }
            return View();
        }
    }
}
